package cn.xiaoniu.day04;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.xiaoniu.day04.bean.JobBean;
import cn.xiaoniu.day04.bean.Page;

public class TestMain {
	public static void main(String[] args) {
		int count = 0;
		String url = "https://search.51job.com/list/170200,000000,0000,00,9,99,%25E5%25A4%25A7%25E6%2595%25B0%25E6%258D%25AE,2,11.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=";
		Document document = getDocumentByUrl(url);
		Page page = new Page();
		page.setDocument(document);
		while(true){
			System.out.println("第"+(++count)+"页");
			List<JobBean> jobByPage = getJobByPage(page);
			//处理job   保存到文件   保存到数据库
			for (JobBean jobBean : jobByPage) {
				System.out.println(jobBean);
			}
			getNextPage(page);
			if(page.isHasNextPage()){//有下一页的时候
				Document documentByUrl = getDocumentByUrl(page.getNextPageUrl());
				page.setDocument(documentByUrl);
			}else{//没有下一页的时候
				break;
			}
			//休眠一下
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取下一页地址，和修正是否有下一页
	 */
	public static void getNextPage(Page page){
		Document document = page.getDocument();
		Elements select = document.select(".bk");
		Element element2 = select.get(1);
		Elements select2 = element2.select("a");//最后一页的时候没有a
		if(select2!=null&&select2.size()>0){//有下一页的时候
			String url = select2.get(0).attr("href");//得到下一页的地址
			page.setHasNextPage(true);
			page.setNextPageUrl(url);
		}else{
			page.setHasNextPage(false);
		}
	}
	
	/**
	 * 通过page获取job岗位数据
	 * 
	 */
	public static List<JobBean> getJobByPage(Page page){
		List<JobBean> list = new ArrayList<>();
		
		//通过document获取岗位集合
		Document document = page.getDocument();
		Elements select = document.select("#resultList .el");
		//剔除标头数据
		select.remove(0);
		for (Element element : select) {
			//获取具体的信息内容
			Elements jobElement = element.select(".t1 a");
			String jobName = jobElement.attr("title");
			String comName = element.select(".t2 a").attr("title");
			String addr = element.select(".t3").text();
			String salary = element.select(".t4").text();
			String date = element.select(".t5").text();
			JobBean jobBean = new JobBean();
			jobBean.set(jobName, comName, addr, salary, date);
			//System.out.println(jobBean);
			list.add(jobBean);
		}
		return list;
	}
	
	/**
	 * 通过url地址获取document
	 */
	public static Document getDocumentByUrl(String url){
		try {
			Document document = Jsoup.parse(new URL(url), 4000);
			return document;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
